<template>
    <div class="cmt-container">
        <h3>发表评论</h3>
        <hr>
        <textarea placeholder="请输入评论内容,最多120字" maxlength="120" v-model="content"></textarea>
        <mt-button type="primary" size="large" @click="publishComment" >发表评论</mt-button>
        <div class="cmt-list">
            <div class="cmt-item" v-for="(item,index) in list" :key="item.id">
                <div class="cmt-title">
                    第{{index+1}}楼 &nbsp;&nbsp; 用户: {{item.user_name}} Publish At: {{item.add_time}}
                </div>
                <div class="cmt-body">
                    {{item.content}}
                </div>
            </div>
        </div>

        <mt-button icon="more" size="large" @click="getMore">更多</mt-button>
    </div>
</template>

<script>
    import { Toast } from 'mint-ui';
export default {
    data:function () {
        return{
            list:[],
            pageIndex:1,
            content:'',
            //id:this.$route.params.id
        }
    },
    created:function(){
      this.getCommentList()
    },
    methods:{
        getCommentList(){
            //TODO：mockjs正则匹配有问题，暂时不加参数
            // this.$http.get('/commnet-list/' + this.id).then(res =>{
            this.$http.get('/commnet-list/').then(res =>{
                if (res.body.code == 200){
                    //每当加载新数据，应该是拼接数据，而不是覆盖
                    // this.list = res.body.data;
                    this.list = this.list.concat( res.body.data);
                }
                else{
                    Toast(res.body.message)
                }
            });
        },
        publishComment(){
            if (this.content.trim().length === 0){
                return Toast('内容不能为空')
            }

            this.$http.post('/publish-comment/').then(res =>{
                if (res.body.code == 200){
                    this.list.unshift({
                        id:9527,
                        content:this.content.trim(),
                        user_name:'sid',
                        add_time:'2019-05-27'
                    });
                    Toast('发表成功')
                }
                else{
                    Toast('发表失败')
                }
            })


        },
        getMore(){
            this.pageIndex++;
            this.getCommentList();
        }
    },
    props:[
        'id'
    ]
}
</script>

<style lang="less">
.cmt-container{
    h3{
        font-size: 18px;
    }
    textarea{
        font-size: 14px;
        height: 85px;
        margin: 0;
    }
    .cmt-list{
        margin: 5px 0;
        .cmt-item{
            font-size: 13px;
            .cmt-title{
                background-color: #ccc;
                line-height: 35px;
            }
            .cmt-body{
                text-indent: 2em;
            }
        }
    }
}
</style>